package com.sm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sm.dao.IManangerDao;
import com.sm.model.Manager;

@Controller
@RequestMapping("/manager")
public class ManagerController {

	@Autowired
	private IManangerDao iManangerDao;

	/**
	 * 修改系统管理员密码
	 */
	@RequestMapping(value = "preupdatetepwd", method = RequestMethod.GET)
	public ModelAndView preUpdatetePwd(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("main");

		String mainPage = "updatepwd.jsp";
		request.setAttribute("mainPage", mainPage);

		return modelAndView;
	}
	
	/**
	 * 系统退出
	 */
	@RequestMapping(value = "logout" , method = RequestMethod.GET)
	public ModelAndView logout(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		session.removeAttribute("username");
		session.invalidate();
		
		return new ModelAndView("login");
	}

	/**
	 * 修改系统管理员密码
	 */
	@RequestMapping(value = "pre", method = RequestMethod.POST)
	public ModelAndView pre(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("main");

		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		String repassword = request.getParameter("repassword");

		Manager user = new Manager(username, repassword);
		
		try {
			iManangerDao.updatePwd(user);
			
			 String mainPage = "updatepwdSuccess.jsp";
			 request.setAttribute("mainPage", mainPage);
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			request.setAttribute("error", "更新失败");
			
		}
		
		return modelAndView;
	}
}
